package elephant.servers.consoleserver.command.container;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import elephant.Container;
import elephant.job.BaseJob;
import elephant.task.BaseTask;
import elephant.util.ObjectPrinter;
import io.itit.itf.okhttp.FastHttpClient;
import io.itit.itf.okhttp.FastHttpClientStat;

/**
 * 
 * @author icecooly
 *
 */
public class ContainerInfo {

	private Container container;
	//
	public ContainerInfo(Container container) {
		this.container=container;
	}
	//
	
	public String getVersion() {
		return Container.VERSION;
	}
	//
	public String getServerName() {
		return container.getServerName();
	}
	//
	
	public Date getStartTime() {
		return container.getStartTime();
	}
	//
	//
	public List<JobInfo> getJobs() {
		List<JobInfo>jobList=new ArrayList<JobInfo>();
		for(BaseJob job:container.getJobManager().getJobs()){
			JobInfo jobInfo=new JobInfo();
			jobInfo.setCronExpression(job.getCronExpression());
			jobInfo.setId(job.getId());
			jobInfo.setNextRunTime(job.getNextRunDate());
			jobInfo.setLastRunTime(job.getLastRunTime());
			jobList.add(jobInfo);
		}
		return jobList;
	}
	//
	
	public boolean runJob(String id){
		return container.runJob(id);
	}
	//
	
	public List<TaskInfo> getTasks() {
		List<TaskInfo>list=new ArrayList<>();
		for(BaseTask task:container.getTaskManager().getTasks()){
			TaskInfo taskInfo=new TaskInfo();
			taskInfo.setId(task.getId());
			taskInfo.setInitialDelay(task.getInitialDelay());
			taskInfo.setPeriod(task.getPeriod());
			taskInfo.setUnit(task.getUnit());
			taskInfo.setRunTimes(task.getRunTimes());
			taskInfo.setLastRunTime(task.getLastRunTime());
			list.add(taskInfo);
		}
		return list;
	}
	//
	
	public boolean runTask(String id){
		return container.runTask(id);
	}
	//
	public String dumpFastHttpClientInfo() {
		ObjectPrinter op=new ObjectPrinter();
		op.section(FastHttpClientStat.class.getSimpleName());
		op.print("version:",FastHttpClient.VERSION);
		op.print("startTime:",FastHttpClientStat.getStartTime());
		op.print("lastAccessTime:",FastHttpClientStat.getLastAccessTime());
		op.print("reqTotalCount:",FastHttpClientStat.getReqTotalCount());
		op.print("reqFailureCount:",FastHttpClientStat.getReqFailureCount());
		op.print("reqExceptionCount:",FastHttpClientStat.getReqExceptionCount());
		//
		if(FastHttpClientStat.getErrorMsgs()!=null&&
				FastHttpClientStat.getErrorMsgs().size()>0){
			op.section("Latest Errors");
			FastHttpClientStat.getErrorMsgs().forEach(msg->{
				op.println(msg);
			});
		}
		return op.toString();
	}
	//
	public String dump() {
		return container.dump();
	}
	
}
